/**
 * <pre>
 * ss-fire-collector / ConfigUtil.java
 * net.apexcns.core.util Class
 * ConfigUtil
 * <br>
 * 
 * <br>
 * @author ytjeong/apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 14.
 * </pre>
 */
package net.apexcns.core.util;

import java.io.File;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import org.apache.log4j.xml.DOMConfigurator;

import collector.core.CollectConstants;

/** <pre>
 * Class : net.apexcns.core.util.ConfigUtil / ConfigUtil.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author ytjeong / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 14.
 */

public class ConfigUtil {
	
	public static String getAppHome() {
		return getConfigBase();
	}
	public static String getConfigBase() {
		return System.getProperty("apex.configBase", System.getProperty("user.dir"));
	}
	/**
	 * 0. app.config
	 * 0. app.home(user.dir) + /conf/config-module.xml
	 * 0. app.home(user.dir) + /conf/config.xml
	 * @return
	 */
	public static String getAppConfigFile() {
		String appConfig = System.getProperty("app.config");
		
		if(appConfig!=null) return appConfig;
		
		appConfig = getAppHome() + "/conf/config-Module.xml";
		if(new File(appConfig).exists()) return appConfig;
		
		return getAppHome() + "/conf/config.xml";
	}

	public static String getHostName() {
		String hostName = null;
//		try {
//			NetInfo info = proxy.getNetInfo();
//			hostName = info.getHostName();
//		} catch (Exception e) {
//		}
		return hostName==null ? getHostNameJava() : hostName;
	}
	
	private static String getHostNameJava() {
		String hostName = null;
		InetAddress address = null;
		try {
			address = InetAddress.getLocalHost();
			hostName = address.getHostName();
		} catch (UnknownHostException e) {
		}
		return hostName;
	}
	
	// 
	@SuppressWarnings("unused")
	private static String getLocalHost() {
		String ipAddr = null;
		try {
			Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
			while(ifaces!=null && ifaces.hasMoreElements()) {
				NetworkInterface iface = ifaces.nextElement();
				if(iface.isLoopback()) continue;
				//if(!ni.isUp()) continue;
				for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
					InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
					if(inetAddr.isLoopbackAddress()) continue;
					if(inetAddr instanceof Inet6Address) continue;
					if(canBind(inetAddr.getHostAddress()))
						return inetAddr.getHostAddress();
				}
				
			}
		} catch (SocketException e) {
		}
		return ipAddr;
	}	
	private static boolean canBind(String ipAddr) {
		try {
			ServerSocket server = new ServerSocket();
			server.bind(new InetSocketAddress(ipAddr,0));
			server.close();
			return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	/**
	 * 0. ancmon.config
	 * 0. ancmon.home(user.dir) + /conf/config-hostname.xml
	 * 0. ancmon.home(user.dir) + /conf/config.xml
	 * @return
	 */
	public static String getCollectorConfigFile() {
		String appConfig = System.getProperty("app.config");
		String collectorMode	= System.getProperty(CollectConstants.DEFAULT_SERVER_MODE_KEYNAME);
		String hostName		= getHostName();
		
		if(appConfig!=null) return appConfig;
		
		appConfig = getAppHome() + "/conf/config-" + collectorMode + "-" + hostName + ".xml";
		if(new File(appConfig).exists()) return appConfig;
		
		appConfig = getAppHome() + "/conf/config-" + hostName + ".xml";
		if(new File(appConfig).exists()) return appConfig;
		
		appConfig = getAppHome() + "/conf/config-" + collectorMode + ".xml";
		if(new File(appConfig).exists()) return appConfig;
		
		return getAppHome() + "/conf/config.xml";
	}

	public static void initLogger() {
		String appHome = getAppHome();
		String collectorMode	= System.getProperty(CollectConstants.DEFAULT_SERVER_MODE_KEYNAME);
		String hostName		= getHostName();
		if(System.getProperty("log4j.configuration")!=null) return;

		String logfile = appHome + "/logs/app" + (collectorMode!=null ? "-"+collectorMode : "")
			+ "-" + hostName + ".log";
		System.setProperty(CollectConstants.DEFAULT_SERVER_LOGFILE_KEYNAME, logfile);

		String log4jXml = appHome + "/conf/log4j-" + collectorMode + "-" + hostName + ".xml";
		if(!new File(log4jXml).exists()) {
			log4jXml = appHome + "/conf/log4j-" + hostName + ".xml";
			if(!new File(log4jXml).exists()) {
				log4jXml = appHome + "/conf/log4j-" + collectorMode + ".xml";
				if(!new File(log4jXml).exists()) {
					log4jXml = appHome + "/conf/log4j.xml";
				}
			}
		}
		DOMConfigurator.configureAndWatch(log4jXml);
	}
	
}
